import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/about',
    name: 'About',
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import(/* webpackChunkName: "login" */ '../views/Login.vue')
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import(/* webpackChunkName: "register" */ '../views/Register.vue')
  },
]

const router = new VueRouter({
  mode: process.env.NODE_ENV == 'dev' ? 'history' : 'hash',
  // base: process.env.BASE_URL,
  routes
})


router.beforeEach((to,from,next)=>{
  console.log(to);
  console.log(document.cookie);
  if(to.path === "/about"){
    if(document.cookie){
      next()
    }else{
      alert('您还没有登录,请先去登录')
      next('/login')
    }
    
  }else{
    next()
  }
  
})

export default router
